﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace nanoCMS.Core.Helpers
{
    public static class UrlHelper
    {
        public static string StringToUrl(string originalString)
        {
            originalString = originalString.ToLower();

            originalString = originalString.Replace("č", "c");
            originalString = originalString.Replace("ć", "c");
            originalString = originalString.Replace("ž", "z");
            originalString = originalString.Replace("š", "s");
            originalString = originalString.Replace("đ", "d");

            bool whiteSpace = true;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < originalString.Length; i++)
            {
                if ((originalString[i] >= 'a' && originalString[i] <= 'z') || (originalString[i] >= '0' && originalString[i] <= '9'))
                {
                    sb.Append(originalString[i]);
                    whiteSpace = false;
                }
                if (char.IsWhiteSpace(originalString[i]) && whiteSpace == false)
                {
                    sb.Append(originalString[i]);
                    whiteSpace = true;
                }
            }
            if (whiteSpace == true)
            {
                sb.Remove(sb.Length - 1, 1);
            }
            originalString = sb.ToString();

            originalString = originalString.Replace(" ", "-");
            originalString = HttpUtility.UrlEncode(originalString);
			  
            return originalString;
        }
    }
}
